<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的信息</title>
    <link rel="stylesheet" href="../../css/element.css">
    <link rel="stylesheet" href="../../css/base.css">

    <style>

    </style>
</head>
<body class="frame-body">
<div id="wrapper" v-cloak>
    <el-row style="margin-bottom: 50px">
        <el-col :span="10" :offset="7">

            <div style="margin: 10px 0">
                <el-card>
                    <el-form :model="user" style="width: 80%; margin: 0 auto">
                        <el-form-item label="用户名" label-width="100px">
                            <el-input v-model="user.username" autocomplete="off" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="邮箱" label-width="100px">
                            <el-input v-model="user.email" autocomplete="off" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="电话" label-width="100px">
                            <el-input v-model="user.phone" autocomplete="off" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" label-width="100px">
                            <el-input show-password v-model="user.password" autocomplete="off" style="width: 80%"></el-input>
                        </el-form-item>
                    </el-form>

                    <div style="padding: 10px 0; text-align: center">
                        <el-button type="primary" @click="save">保 存</el-button>
                    </div>
                </el-card>


            </div>

        </el-col>
    </el-row>



</div>

<!--    js-->
<script src="../../js/jquery.min.js"></script>

<script src="../../js/vue.min.js"></script>
<script src="../../js/element.js"></script>

<script>
    new Vue({
        el: "#wrapper",
        data: {
            user: {},
        },
        created() {
            $.ajax({url: '/api/user/session', method: "GET", async: false, success: (res) => {this.user = res.data || {}}})
            this.loadTable()
        },
        methods: {
            logout() {
                $.get("/api/user/logout");
                sessionStorage.removeItem("user");
                location.href = "/page/end/login.html";
            },
            loadTable() {
                if(!this.user.id) {
                    this.$message({
                        message: "请登录！",
                        type: "warning"
                    })
                    return
                }
                $.get("/api/user/" + this.user.id).then(res => {
                    this.user = res.data
                })
            },
            save() {
                $.ajax({
                    url: "/api/user",
                    type: "PUT",
                    contentType: "application/json",
                    data: JSON.stringify(this.user)
                }).then(res => {
                    if (res.code === '0') {
                        this.$message({
                            message: "保存成功",
                            type: "success"
                        });
                        this.load();
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        })
                    }
                })
            },
        }
    })
</script>
</body>
</html>
